package cn.yuli.redis.service.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.yuli.redis.annotation.CacheResult;
import cn.yuli.redis.mapper.UserMapper;
import cn.yuli.redis.model.User;
import cn.yuli.redis.service.UserService;
@Service
public class UserServiceImpl3 implements UserService {
	public static Logger logger = Logger.getLogger(UserServiceImpl3.class);
	@Autowired
	private UserMapper userMpper;//usermapper接口
	
	private static final String CACHE_NAME = "userCache";
	private static final String CACHE_BACKUP_NAME ="user_backup";
	@Transactional
	@CacheResult(key="#user.id",cacheName=CACHE_NAME,backupKey=CACHE_BACKUP_NAME)
	public User addUser(User user) {
		userMpper.insert(user);
		logger.info("向数据库添加用户");
		return user;
	}
	@Transactional
	public void deleteUser(String userId) {
		userMpper.deleteByPrimaryKey(userId);
		
	}
	@Transactional
	public User updateUser(User user) {
		userMpper.updateByPrimaryKeySelective(user);
		return user;
	}
	@Transactional(readOnly=true)
	@CacheResult(key="#userId",cacheName=CACHE_NAME,backupKey=CACHE_BACKUP_NAME,needBloomFilter=true,needLock=true)
	public User queryUser(String userId) {
		logger.info("从数据库里取得用户");
		User user = userMpper.selectByPrimaryKey(userId);
		return user;
	}

}
